 package com.glorysoft.tonglingxjpda.base;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import com.glorysoft.tonglingxjpda.utils.FileUtils;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_InitUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.io.File;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbSession;
import okhttp3.OkHttpClient;
import rxhttp.wrapper.cookie.CookieStore;
import rxhttp.wrapper.param.RxHttp;
import rxhttp.wrapper.ssl.HttpsUtils;


 /**
 * FileName: MyApp
 * Author: XJH
 * Date: 2020/5/12 11:30
 * Description:
 * History:
 */
public class MyApp extends Application {

    public static Context app;
    private static MyApp instance;


    public static MyApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        app = getApplicationContext();
        FreeUI_InitUtils.init(getApplicationContext(), this, true,60*1000L,0);
        FreeUI_InitUtils.setTheme(FreeApi_StaticMembers.THEME_GLORY);
        initRxHttp();
        System.setProperty("jcifs.smb.client.dfs.disabled", "true");
        System.setProperty("jcifs.smb.client.soTimeout", "1000000");
        System.setProperty("jcifs.smb.client.responseTimeout", "30000");

        String localPathAndFileName = FileUtils.Companion.getLocalPathAndFileName(app);
        if (!new File(localPathAndFileName).isDirectory()){
            new File(localPathAndFileName).mkdirs();
            new File(localPathAndFileName).mkdir();
        }
    }

     private void initRxHttp() {
         File file = new File(getExternalCacheDir(), "RxHttpCookie");
         HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory();
         OkHttpClient client = new OkHttpClient.Builder()
                 .cookieJar(new CookieStore(file))
                 .connectTimeout(60, TimeUnit.SECONDS)
                 .readTimeout(60, TimeUnit.SECONDS)
                 .writeTimeout(60, TimeUnit.SECONDS)
                 .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) //添加信任证书
                 .hostnameVerifier((hostname, session) -> true) //忽略host验证
                 .build();
         //RxHttp初始化，自定义OkHttpClient对象，非必须
         RxHttp.init(client);
         RxHttp.setDebug(true);
         RxHttp.setOnParamAssembly(p -> {
            /*根据不同请求添加不同参数，子线程执行，每次发送请求前都会被回调
            如果希望部分请求不回调这里，发请求前调用Param.setAssemblyEnabled(false)即可
             */
             return p; //添加公共请求头
         });
     }
 }
